package com.bt.lms.network.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bt.lms.constant.SysBizTypeConstant;
import com.bt.lms.entity.StudyCen;
import com.bt.lms.entity.page.Page;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.service.StudyCenService;
import com.bt.lms.service.impl.StudyCenServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;

/**
 * 学习中心模块控制器
 *
 * @author 徐大永
 */
public class StudyCenModuleHandler implements ModuleHandler {
    @Override
    public void handlerMsg(ReqMsg reqMsg, Socket socket) {
        try {
            //1.获取类型
            short bizType = reqMsg.getBizType();
            //2.获取数据
            String data = reqMsg.getData();
            //3.适配
            Object obj = adapter(bizType, data);
            //4.请求类型是否需要响应
            byte twoWay = reqMsg.getTwoWay();
            if (twoWay == ReqMsg.NETWORK_TWO_WAY_CHAT) {
                if (obj == null) {
                    //无返回内容
                    resp(socket, reqMsg);
                    socket.close();
                } else {
                    //4.5 响应
                    resp(socket, reqMsg, obj);
                    //结束
                    socket.close();
                }
            } else {
                //结束
                socket.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 响应
     *
     * @param socket 客户端
     * @param obj    响应对象
     */
    private void resp(Socket socket, ReqMsg reqMsg, Object obj) {


        try {

            RespMsg respMsg = new RespMsg();
            BeanUtils.copyProperties(respMsg, reqMsg);
            String data = JSON.toJSONString(obj);
            respMsg.setData(data);
            //状态
            respMsg.setStatus(RespMsg.NETWORK_STATUS_NORMAL);

            //响应消息转字节流
            byte[] respBytes = respMsg.toBytes();
            OutputStream out = socket.getOutputStream();
            out.write(respBytes);
            out.flush();

            out.close();
        } catch (IOException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 响应（无返回值）
     *
     * @param socket 客户端
     */
    private void resp(Socket socket, ReqMsg reqMsg) {

        try {

            RespMsg respMsg = new RespMsg();
            BeanUtils.copyProperties(respMsg, reqMsg);
            //状态
            respMsg.setStatus(RespMsg.NETWORK_STATUS_NO_BIZ);

            //响应消息转字节流
            byte[] respBytes = respMsg.toBytes();
            OutputStream out = socket.getOutputStream();
            out.write(respBytes);
            out.flush();

            out.close();
        } catch (IOException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 适配
     *
     * @param bizType 业务类型
     * @param data    数据
     * @return 业务返回值
     */
    private Object adapter(short bizType, String data) {

        //要返回的对象
        Object obj = null;

        StudyCenService studyCenService = new StudyCenServiceImpl();
        //开始适配
        if (bizType == SysBizTypeConstant.STUDY_CEN_PAGE_QUERY) {

            Page<StudyCen> objectPage = new Page<>();
            obj = studyCenService.pagingQuery(objectPage);

        } else if (bizType == SysBizTypeConstant.STUDY_CEN_ADD) {

            StudyCen studyCen = JSONObject.parseObject(data, StudyCen.class);
            obj = studyCenService.add(studyCen);

        } else if (bizType == SysBizTypeConstant.STUDY_CEN_DELETE) {

            StudyCen studyCen = JSONObject.parseObject(data, StudyCen.class);
            Long studyCenId = studyCen.getId();
            obj = studyCenService.del(studyCenId);

        } else if (bizType == SysBizTypeConstant.STUDY_CEN_COMPILE) {

            StudyCen studyCen = JSONObject.parseObject(data, StudyCen.class);
            obj = studyCenService.compile(studyCen);

        } else if (bizType == SysBizTypeConstant.STUDY_CEN_DETAILS) {

            StudyCen studyCen = JSONObject.parseObject(data, StudyCen.class);
            Long studyCenId = studyCen.getId();
            obj = studyCenService.detail(studyCenId);

        }

        return obj;
    }
}
